package com.sskj.mine.ui.activity;

import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.SettingPasswordPresenter;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_SET_PAY_PWD)
public class SettingPasswordActivity extends BaseActivity<SettingPasswordPresenter> {

    @BindView(R2.id.password_name)
    TextView passwordName;
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.old_pwd)
    EditText oldPwd;
    @BindView(R2.id.password_name2)
    TextView passwordName2;
    @BindView(R2.id.password_edt2)
    EditText passwordEdt2;

    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @Inject
    UserViewModel userViewModel;

    /**
     * 0-> 修改登录密码
     * 1->设置资金密码
     * 2->修改资金密码
     */
    @Autowired
    int type = 1;
    String mobile;
    String name;
    String name2;
    String hint;
    String hint2;

    @BindView(R2.id.old_pwd_ll)
    LinearLayout oldPwdLl;
    private UserData userData;
    private BottomSheetDialog bottomSheetDialog;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_setting_password;
    }

    @Override
    public SettingPasswordPresenter getPresenter() {
        return new SettingPasswordPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);

        ARouter.getInstance().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mobile = userData.getPhone();
            } else {
                userData = null;
            }
        });
        switch (type) {
            case 0:
                oldPwd.setHint(App.INSTANCE.getString(R.string.mine_settingPasswordActivity1));

                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity2));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity3);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity4);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5);
                hint2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity6);
                oldPwdLl.setVisibility(View.VISIBLE);


                break;
            case 1:
                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity7));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity8);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity4);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5);
                hint2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity6);
                oldPwdLl.setVisibility(View.GONE);

                break;
            case 2:
                oldPwd.setHint(App.INSTANCE.getString(R.string.mine_settingPasswordActivity9));
                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity10));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity8);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity4);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5);
                hint2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity6);
                oldPwdLl.setVisibility(View.VISIBLE);

                break;
        }
        passwordName.setText(name);
        passwordName2.setText(name2);
        passwordEdt.setHint(hint);
        passwordEdt2.setHint(hint2);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {


        ClickUtil.click(submitBtn, () -> {
            if (type == 0) {
                if (isEmpty(oldPwd)) {
                    ToastUtil.showShort(hint);
                    return;
                }
            }
            if (isEmpty(passwordEdt)) {
                ToastUtil.showShort(hint);
                return;
            }
            if (isEmpty(passwordEdt2)) {
                ToastUtil.showShort(hint2);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(passwordEdt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity11));
                return;
            }
            if (!getText(passwordEdt).equals(getText(passwordEdt2))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity12));
                return;
            }


            bottomSheetDialog = BottomSheetUtil.showCheck(this, false, userData.getConfig().getGoogle_verify() == 1, userData.getConfig().getSms_verify() == 1, str -> {

                bottomSheetDialog.dismiss();
                if (type == 1) {
                    mPresenter.setPayPwd(getText(passwordEdt), getText(passwordEdt2), str[2], str[1]);
                } else {
                    mPresenter.updatePayPwdOrLoginPwd(getText(oldPwd), getText(passwordEdt), str[2], type == 2, str[1]);
                }
            });
            bottomSheetDialog.show();
        });
    }


    public void updataUI() {
        if (type == 0) {
            LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
            logoutProvider.logout();
        } else {
            userViewModel.update();
            finish();
        }
    }

}
